<?php

namespace app\lib\MessageBus\works;

use app\common\model\book\Book;
use app\common\model\click\UserCallback;
use app\common\model\media_account\MediaAccount;
use app\common\model\risk_report\RiskReport;
use app\common\model\user\User;
use app\common\model\click\UserCallbackLog;
use app\common\model\promotion\PromotionLink;
use app\common\model\recycle_report\RecycleReport;
use app\lib\Instance;
use app\lib\MessageBus\base\AbstractWork;

//用户注册回传信息获取
class MemberLoginCallBackData extends AbstractWork
{
    public function handler($message)
    {
        dump("用户callback打印开始 用户ID-" . $message['user_id'] . date("Y-m-d H:i:s"), $message);
        $userCallback = UserCallback::where('user_id', $message['user_id'])->findOrEmpty();

        if ($userCallback->isEmpty()) {
            $userCallback = UserCallback::create($message["user_callback"]);
        } else {
            $userCallback->save($message["user_callback"]);
        }
        //如果推广链ID不为0 增加回传日志 用户推广日报增加1
        if (isset($message["promotion_link_id"])) {
            $PromotionLink = PromotionLink::where(['id' => $message["promotion_link_id"]])->findOrEmpty();
            if (!$PromotionLink->isEmpty()) {
                $channel_type = Book::where('id', $PromotionLink->book_id)->value('channel_type') ?? 1;
                User::where('id', $message['user_id'])->update(['sex' => $channel_type]);//根据推广链书籍修改用户性别
                $PromotionLink->add_fans += 1;
                //新增人数大于5人时，将推广链状态设置为在跑状态
                if ($PromotionLink->add_fans >= 5 && $PromotionLink->status == 0) {
                    $PromotionLink->status = 1;
                }
                $PromotionLink->save();
            }
            $count_date    = date('Y-m-d', time());
            $hour          = date('H');
//            if (UserCallbackLog::where(['user_id' => $message['user_id'], 'promotion_link_id' => $message["promotion_link_id"]])->findOrEmpty()->isEmpty()) {
            //不校验记录是否存在，推广链传过来就插入一条，应对同一推广链用户满足归因二次进入，其他地方用的时候，取最新染色时间的记录
                UserCallbackLog::create([
                    'user_id'           => $message['user_id'],
                    'promotion_link_id' => $message['promotion_link_id'],
                    'active_status'     => 1,
                    'dyeing_time'       => $message["user_callback"]['dyeing_time'],
                ]);
//            }else{
//                UserCallbackLog::where(['user_id' => $message['user_id'], 'promotion_link_id' => $message["promotion_link_id"]])->update([
//                    'user_id'           => $message['user_id'],
//                    'promotion_link_id' => $message['promotion_link_id'],
//                    'active_status'     => 1,
//                    'dyeing_time'       => $message["user_callback"]['dyeing_time'],
//                ]);
//            }
            //判断当日推广链报表是否存在，不存在就创建
            $recycleReport = RecycleReport::where(['count_date' => $count_date, 'promotion_link_id' => $message["promotion_link_id"]])->findOrEmpty();
            if (!$recycleReport->isEmpty()) {
                $recycleReport->new_fans_num += 1;
                //查询用户是不是今日注册
                $todayTimestamp = strtotime('today');
                $todayUser = User::where('id',$message['user_id'])->where('create_time', '>=', $todayTimestamp)->where('create_time', '<', $todayTimestamp + 86400)->findOrEmpty();
                if (!$todayUser->isEmpty()){
                    $recycleReport->add_new_fans_num += 1;
                }
                $recycleReport->save();
            } else {
                RecycleReport::create([
                    'count_date'        => $count_date,
                    'promotion_link_id' => $message["promotion_link_id"],
                    'new_fans_num'      => 1,
                    'add_new_fans_num'      => 1,
                    'pitcher_id'        => $PromotionLink->admin_id ?? 0,
                ]);
            }
            $riskReport = RiskReport::where([
                'count_date' => $count_date, 'promotion_link_id' => $message["promotion_link_id"], 'hour' => $hour
            ])->findOrEmpty();
            if (!$riskReport->isEmpty()) {
                $riskReport->new_fans_num += 1;
                $riskReport->save();
            } else {
                $adId = MediaAccount::where('promotion_link_id', $PromotionLink->id)->order('id', 'desc')->value('advertiser_id');
                RiskReport::create([
                    'count_date'        => $count_date,
                    'hour'              => $hour,
                    'promotion_link_id' => $message["promotion_link_id"],
                    'pitcher_id'        => $PromotionLink->admin_id ?? 0,
                    'advertiser_id'     => $adId ?? 0,
                    'new_fans_num'      => 1,
                ]);
            }
        }
        dump("用户callback打印结束 用户ID-" . $message['user_id'] . date("Y-m-d H:i:s"), $message);
    }

}
